/*******************************************************************************
**                                                                            **
**                       Helper Functions                                     **
**                                                                            **
*******************************************************************************/

//this function gets the elements by class name in a node or throughout the
//document
//
// searchClass       class to search for. This parameter is required
// node              node to search in. If null, the entire document will
//                      be searched
// tag               tag to look for within the node or document. If null, all
//                      tags will be searched
// return            array of elements found
function getElementsByClass(searchClass,node,tag)
{

   var classElements = new Array();

   //if Node is NULL search the entire document
   if ( node == null )
   {
      node = document;
   }

   //if the tag is null search for everything
   if ( tag == null )
   {
      tag = '*';
   }

   //get everything in the node with the tag name
   var els = node.getElementsByTagName(tag);

   //set the pattern to search for the beginning of the string or a white space
   //followed by the search class followed by a whitespace or the end of the
   //string
   var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

   //loop through each element and see if the className matches the search class
   //if it does then add it to the array
   for (i = 0, j = 0; i < els.length; i++)
   {
      if ( pattern.test(els[i].className) )
      {
         classElements[j] = els[i];
         j++;
      }
   }

   return classElements;
}

//This is a function that handles the event of a click. This function inserts
//the information into the text box for a reply, edit or a PM
//
// event             event that occured
function insertAtCursor(event)
{
   event.preventDefault();
   var link, textarea, openTag, closeTag;
   link = event.currentTarget;
   openTag = '['+link.title+']';
   closeTag = '[/'+link.title+']';
   var textareas = document.getElementsByTagName('textarea');
   textarea = textareas[0];
   var scrollX = textarea.scrollLeft;
   var scrollY = textarea.scrollTop;
   if (textarea.selectionStart || textarea.selectionStart == '0')
   {
      var startPos = textarea.selectionStart;
      var endPos = textarea.selectionEnd;
      textarea.value = textarea.value.substring(0, startPos)
         + openTag
         + textarea.value.substring(
            textarea.selectionStart,
            textarea.selectionEnd
            )
         + closeTag
         + textarea.value.substring(
            endPos,
            textarea.value.length
            );

      if (startPos == endPos)
      {
         textarea.selectionStart = endPos + openTag.length;
      }
      else
      {
         textarea.selectionStart = endPos + openTag.length + closeTag.length;
      }

      textarea.selectionEnd = textarea.selectionStart;
   }

   textarea.focus();
   textarea.scrollLeft = scrollX;
   textarea.scrollTop = scrollY;
}

//This function creates the buttons in the reply, edit, or PM window. The button
//that is created will connect to an event that inserts it into the textArea
//
// imageSource       source file or data for the image.
// title             what title the button should have. This will be inserted
//                   in the text area when the button is selected
// return            the created button
function createButton(imageSource,title)
{
   var image, button;
   image = document.createElement('img');
   image.src =  imageSource;
   image.style.backgroundColor = '#5a121e';
   image.style.marginTop = 2;
   image.style.marginLeft = 2;

   image.addEventListener(
      'mouseover',
      function (event)
         {
            this.style.backgroundColor = '#000000';
         },
      false
      );

   image.addEventListener(
      'mouseout',
      function (event)
         {
            this.style.backgroundColor = '#5a121e';
         },
      false
      );

   button = document.createElement('a');
   button.title = title;
   button.href = title;
   button.addEventListener('click', insertAtCursor, false);
   button.appendChild(image);
   return button;
}

//this is a helper function to set the node color for the background and border
//
// node              node to color
// color             color to set the node to
function setNodeColor(node, color)
{
   node.style.backgroundColor = color;
   node.style.borderColor = color;
}

//this is a helper function to set the font weight of a node
//
// node              node to set the font weight
// fw                font weight to set
function setFontWeight(node, fw)
{
   node.style.fontWeight = fw;
}

//this is a helper function to insert an image before a location
//
// imgSrc            source file or data to use as image
// loc               location to insert before
function insertImageBefore(imgSrc, loc)
{
   var myImg = document.createElement('img');
   myImg.src = imgSrc;

   loc.parentNode.insertBefore(myImg, loc.nextSibling);
}

//This function gets the child node list from the grandparent node. This was
//simply a very common operation so this function simplifys the code a bit
//
// node              node to get the child node list from the grandparent node
// return            grandparent node's child node list
function childNodeList(node)
{
   return node.parentNode.parentNode.childNodes;
}

//This function sets the color for a watchlist.
//
// list              list to search for a match
// node              node to match against
// color             color to set in the case of a match
function colorList(list, node, color)
{
   //go through the list
   for(j=0; j<list.length; j++)
   {
      //make sure to set the names to uppercase so we aren't case sensitive
      if ( list[j].toUpperCase() ==
         childNodeList(node)[3].innerHTML.toUpperCase())
      {
         for(k=0; k < childNodeList(node).length; k++)
         {
            setNodeColor(
               childNodeList(node)[k],
               color
               );

            setFontWeight(
               childNodeList(node)[k],
               'bold'
               );
         }
      }
   }
}

//This function will restore the default list
function loadDefaultWatchList()
{
   localStorage.ls_watchListArray = JSON.stringify(userWatchList);
   localStorage.ls_restoreDefault = "false";
}